/**
 * @author kristian
 */
/*
(For Hex Maps, use the same algorithm, but with the following MouseMap:  )

    * First Step: Find out what region of the map the mouse is in.

      RegionX=int(MouseX/MouseMapWidth)
      RegionY=int(MouseY/MouseMapHeight)*2 {The multiplying by two is very important}

    * Second Step: Find out WHERE in the mousemap our mouse is, by finding MouseMapX and MouseMapY.

      MouseMapX=MouseX MOD MouseMapWidth
      MouseMapY=MouseY MOD MouseMapHeight

    * Third Step: Determine the color in the MouseMap at (MouseMapX,MouseMapY).

    * Fourth Step: Find RegionDX and RegionDY in the following table:

      Color 	RegionDX 	RegionDY
      RED 		-1 				-1
      YELLOW 	 0 				-1
      WHITE 	 0 				 0
      GREEN 	-1 				 1
      BLUE 		 0				 1

    * Fifth Step: Use RegionX,RegionY, RegionDX, and RegionDY to find out TileX and TileY

      TileX=RegionX+RegionDX
      TileY=RegionY+RegionDY


var calcHexCoordsFromMouseCoords = function(mousePoint, mouseMapSize) {
	var mouseX = mousePoint.x;
	var mouseY = mousePoint.y;
	var regionX = parseInt(mouseX / mouseMapSize.width);
	var regionY = parseInt(mouseY / mouseMapSize.height) * 2;
    var mouseMapX = mouseX % mouseMapSize.width;
    var mouseMapY = mouseY % mouseMapSize.height;			
}
*/

/**
 * 
 * (mx,my) = mouse coordinate : mouseX, mouseY
 * h = height of hexagon
 * tw = width of hexagon horizontal side
 * cw = width of side triangle
 */
var calcHexCoordsFromMouseCoords = function(mousePoint, scale) {
	var mx = mousePoint.x;
	var my = mousePoint.y;
	var cw = parseInt(scale.x / 4);
	var h = scale.y;
	var tw = parseInt(scale.x / 2);
	console.log('cw:' + cw);
	var tx = mx / (cw + tw); 
	console.log('tx:' + tx);
	var rx = mx % (cw + tw);
	console.log('rx:' + rx);	
	my += (tx * h / 2);
	var ty = my / h; 
	var ry = my % h;
	var rx = tw + cw - rx;
	ry = ry - (h/ 2);
	if(2* cw * ry > rx * h) {
		tx++; 
		ty++;
	}
	if(2 * cw * ry < -rx * h) {
		tx++;
	}
	console.log('tx:' + tx);
	console.log('ty:' + ty);	
	return {x: tx, y: ty};
}

var oldCalcHex = function(ev, options){
	var screenY = ev._y - 14;
	var screenX = Math.round(ev._x - (options.scale.x - 20));
	
	if (screenX < 0) 
		screenX = 0;
	if (screenY < 0) 
		screenY = 0;
	if (screenX > 400) 
		screenX = 400;
	if (screenY > 400) 
		screenY = 400;
	
	// console.log('screenX:' + screenX + ', x:' + ev._x);
	// console.log('screenY:' + screenY + ', y:' + ev._y);
	
	
	boxX = screenX;
	boxY = screenY;
	// console.log('screenX:' + screenX, ', screenY:' + screenY);
	if (screenX > 0) {
		var boxX = Math.round((screenX / options.scale.x));
	}
	if (screenY > 0) {
		var boxY = Math.round((screenY / options.scale.y));
	}
	
	boxXFinal = boxX * options.scale.x;
	boxYFinal = boxY * options.scale.y;
	
	// console.log('boxX:' + boxX, ', boxY:' + boxY);
	// console.log('box X Final:' + boxXFinal, ', box Y Final:' + boxYFinal);
	
	var hexX = parseInt(boxXFinal / options.scale.x);
	var hexY = parseInt(boxYFinal / options.scale.y);
	// console.log('hexX:' + hexX, ', hexY:' + hexY);
	return {hexX: hexX, hexY: hexY};
}